<?php
	// Une classe pour gérer les dates
	// Auteur : Sebastian Mosser
	
	
class Date {
	
	private $_y;
	private $_m;
	private $_d;
	
	public static $days = array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi",
															"Samedi", "Dimanche");
	
	
	public static function getDaysAsOption($selected = -1) {
		$result = "";
		foreach (self::$days as $k => $v) {
			if ($k == $selected)
				$result .= "<option value=\"$k\" selected > $v </option>";
			else
				$result .= "<option value=\"$k\" > $v </option>";
		}
		return $result;
	}
	
	
	public static function isValidTime($string) {
		if (strlen($string) != 5)
			return False;
		if ($string[2] != ":")
			return False;
		if (! is_numeric(substr($string,0,2)))
			return False;
		if (! is_numeric(substr($string,3,2)))
			return False;
		if (substr($string,0,2) < 0 or substr($string,0,2) > 23)
			return False;
		if (substr($string,3,2) < 0 or substr($string,3,2) > 60)
			return False;
		return True;
	}
	
	private static $months = array("Janvier", 
																 "Février", 
																 "Mars", 
																 "Avril", 
																 "Mai", 
																 "Juin", 
																 "Juillet", 
																 "Aout", 
																 "Septembre", 
																 "Octobre", 
																 "Novembre", 
																 "Décembre"); 

	// constructeur de Date
	public function __construct($y,$m,$d) {
		$this->_y = $y;
		$this->_m = $m;
		$this->_d = $d;
	}



	public function isUnknownDate() {
		return $this->_y == "0000" && $this->_m = "00" && $this->_d = "00";
	}
	
	public static function buildUnknownDate() {
		$date = new Date("0000","00","00");
		return $date;
	}

	public function getYear()  { return $this->_y; }
	
	public function getMonth() { return $this->_m; }
	
	public function getDay()   { return $this->_d; }
	
	public function sqlFormat() {
		$y = $this->_y;
		$m = ((strlen($this->_m) == 1) ? "0".$this->_m:$this->_m);
		$d = ((strlen($this->_d) == 1) ? "0".$this->_d:$this->_d);
		return $y . "-" . $m . "-" . $d ;
	}
	
	// Affiche la date sous forme lisible ... :'( 
	public function prettyPrint() {
		$m = self::$months[$this->_m - 1];
		return $this->_d . " " . $m . " " . $this->_y;
	}
	
	// Affiche la date sous forme abrégée ... :'( 
	public function lightPrint() {
		return $this->_d . "/" . $this->_m . "/" . $this->_y;
	}
	
	// Fabrique une date a partir de sa representation SQL
	public static function fromSql($sqlString) {
		$tmp = explode("-",$sqlString);
		return new Date($tmp[0],$tmp[1],$tmp[2]);
	}
	
	// Check si une date au format SQL est valide :
	public static function isValidDate($str) {
		if (strlen($str) != 10)
			return false;
			
	  $tmp = explode("-",$str);
	  if (count($tmp) != 3)
	  	return false;
	  
	  if (strlen($tmp[0]) != 4)
	  	return false;
	  if (strlen($tmp[1]) != 2)
	  	return false;
	  if (strlen($tmp[2]) != 2)
	  	return false;
	  	
	  return checkDate($tmp[1],$tmp[2],$tmp[0]);
	  
	}
	
	public function displaySquizzed() {
		return $this->_d . "/" . $this->_m . "/" .$this->_y;
	}
	
	// Affiche une date en Bajax
	public static function bajax_displayDate($array) {
		$d = new Date($array[0],$array[1],$array[2]);
		return $d->prettyPrint();
	}
}


?>